/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

/**
 *
 * @author samuel
 */
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public abstract class DAOGenerics<K, G> {

    protected void inserir(K obj) {

        SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
        Session s = sf.openSession();

        s.getTransaction().begin();

        s.saveOrUpdate(obj);
        s.getTransaction().commit();
        s.close();
    }

    protected void apagar(K obj, Session s) {

        s.getTransaction().begin();
        s.delete(obj);
        s.getTransaction().commit();

    }

    protected void apagar(K obj) {

        SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
        Session s = sf.openSession();

        try {
            apagar(obj, s);
        } finally {
            s.close();
        }
    }

    protected abstract K buscar(G obj);

    protected abstract K buscar(G obj, Session s);

    protected abstract List<K> buscarTodos();

    protected abstract List<K> buscarTodos(Session s);
}
